#include "../headers/qtexedit.h"
#include <QDebug>

Completor::Completor(QWidget *parent):
    QListWidget(parent)
{
    theEdit=NULL;
}

void Completor::keyPressEvent ( QKeyEvent * e ){
    if(e->key() == Qt::Key_Enter){
        qDebug()<<"{Completor::keyPressEven()}Enter is pressed."<<endl;
    }else if(e->key() == Qt::Key_Space){
        //qDebug()<<"{Completor::keyPressEven()}Space is pressed."<<endl;
        QListWidgetItem* it = this->currentItem();
        emit(submit(it));
    }else if(e->key() == Qt::Key_Down){
        if(currentRow() >= count()-1){
            setCurrentRow(0);
            return;
        }
    }else if(e->key() == Qt::Key_Up){
        if(currentRow() == 0){
            setCurrentRow(count()-1);
            return;
        }
    }else if(e->key() == Qt::Key_K || e->key() == Qt::Key_E){
        if(currentRow() == 0){
            setCurrentRow(count()-1);
            return;
        }else{
            setCurrentRow(currentRow()-1);
            return;
        }
    }else if(e->key() == Qt::Key_J || e->key() == Qt::Key_D){
        if(currentRow() >= count()-1){
            setCurrentRow(0);
            return;
        }else{
            setCurrentRow(currentRow()+1);
            return;
        }
    }else if(e->key() == Qt::Key_Tab){
        if(theEdit!=NULL){
            theEdit->setFocus();
        }
        this->clear();
        this->hide();
        return;
    }
    QListWidget::keyPressEvent(e);
}
void Completor::keyReleaseEvent ( QKeyEvent * e ){
    QListWidget::keyReleaseEvent(e);
}


QTeXEdit::QTeXEdit(QWidget *parent) :
    QPlainTextEdit(parent)
{
    cmp = NULL;
    sChecker = NULL;
}

void QTeXEdit::applySpellDict(QString dicName, QString path){
    if(sChecker!=NULL){
        delete sChecker;
        sChecker = NULL;
    }
    QString affpath= path+dicName+".aff";
    QString dicpath= path+dicName+".dic";
    sChecker = new Hunspell(affpath.toLatin1(),dicpath.toLatin1());
}

void QTeXEdit::setCompleter(Completor* w){
    cmp = w;
    w->theEdit = this;
    //connect(cmp,SIGNAL(itemActivated(QListWidgetItem*)),this,SLOT(activateItem(QListWidgetItem*)));
    connect(cmp, SIGNAL(submit(QListWidgetItem*)),this,SLOT(activateItem(QListWidgetItem*)));
}

void QTeXEdit::keyPressEvent ( QKeyEvent * e ){
    if(cmp!=NULL && (! cmp->isHidden())){
        cmp->setCurrentRow(0);
        if(e->key()==Qt::Key_Up
                || (e->key()== Qt::Key_K && e->modifiers()==Qt::ControlModifier)
                ){
                cmp->setFocus();
                cmp->setCurrentRow(cmp->count()-1);
                return;
        }else if(e->key()==Qt::Key_Down
                 || (e->key()== Qt::Key_J && e->modifiers()==Qt::ControlModifier)
                     ||e->key()== Qt::Key_Tab

                 ){
                cmp->setFocus();
                cmp->setCurrentRow(0);
                return;
        }else if(e->key()==Qt::Key_Left || e->key()== Qt::Key_Home || e->key()== Qt::Key_End ||
                 e->key()== Qt::Key_PageDown || e->key()== Qt::Key_PageUp
                 ){
            cmp->clear();
            cmp->hide();
            this->setFocus();
            return;
        }else if(e->key()==Qt::Key_Right){
            QString txt = cmp->currentItem()->text();
            emit(askToComplete(txt));
            this->setFocus();
            cmp->clear();
            cmp->hide();
            this->setFocus();
            return;
        }
        else if(e->key()  == Qt::Key_Space){
            QString txt = cmp->currentItem()->text();
            emit(askToComplete(txt));
            this->setFocus();
            return;
        }else if(e->key() == Qt::Key_Escape){
            cmp->clear();
            cmp->hide();
            this->setFocus();
            return;
        }

    }

    QPlainTextEdit::keyPressEvent(e);
}

void QTeXEdit::keyReleaseEvent ( QKeyEvent * e ){
    QPlainTextEdit::keyReleaseEvent(e);
}

void QTeXEdit::activateItem(QListWidgetItem* it){
    QString txt = it->text();
    emit(askToComplete(txt));
}

void QTeXEdit::inputMethodEvent ( QInputMethodEvent * e ){
    //qDebug()<<"Commiting String: {"<<e->commitString()<<"}"<<endl;
    QPlainTextEdit::inputMethodEvent(e);
}

void QTeXEdit::mousePressEvent(QMouseEvent *e){
    if(cmp!=NULL && (! cmp->isHidden())){
        cmp->clear();
        cmp->hide();
        this->setFocus();
    }
    QPlainTextEdit::mousePressEvent(e);
}
